<?php

declare(strict_types=1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class PostCollect extends Model
{
  //

  public static function option($user_id, $post_id, $status)
  {
    if ($status == 1) {
      return self::do($user_id, $post_id);
    }
    return self::dont($user_id, $post_id);
  }
  public static function do($user_id, $post_id)
  {
    PostCollect::create([
      'user_id' => $user_id,
      'post_id' => $post_id
    ]);

    $model_post = Post::find($post_id);

    $model_post->collect = $model_post->collect + 1;
    $model_post->save();
    return 1;
  }
  public static function dont($user_id, $post_id)
  {
    PostCollect::where([
      'user_id' => $user_id,
      'post_id' => $post_id
    ])->delete();
    $model_post = Post::find($post_id);

    $model_post->collect = $model_post->collect - 1;
    $model_post->save();
    return 0;
  }
}
